from question_classifier_ekg import *
from question_parser_ekg import *
from py2neo import Graph


class AnswerSearcher:
    def __init__(self):
        # self.g = Graph(
        #     host="127.0.0.1",
        #     http_port=7474,
        #     user="neo4j",
        #     password="gu13569740552")
        self.g = Graph('http://localhost:7474', user='neo4j', password='gu13569740552')

    '''答案搜索主函数'''
    '''执行cypher查询，并返回相应结果'''

    def search_main(self, res_classify, sqls):
        answers = []
        other = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            if 'goods_brand' in question_type:
                other = list(res_classify['args'].keys())
            queries = sql_['sql']
            for query in queries:
                ress = self.g.run(query).data()
                for res in ress:
                    answers.append(res['m.name'])
            # final_answer = self.answer_prettify(question_type, answers)
            # if final_answer:
            #     final_answers.append(final_answer)

        res_json = {"res": answers, "other": other}
        return res_json


'''测试'''

if __name__ == '__main__':
    classifier = QuestClassifier()
    parser = QuestParser()
    searcher = AnswerSearcher()
    res_classify = classifier.classify(input("智能搜索："))
    print(res_classify)
    res_sql = parser.parser_main(res_classify)
    print(res_sql)
    res_json = searcher.search_main(res_classify, res_sql)
    print(res_json)
